//获取当前定位

// #ifdef APP-PLUS
import permision from "@/common/utils/permission.js";
// #endif
// #ifndef APP-PLUS
import amap from "@/common/libs/gaode/amap-wx.js";
// #endif

export async function getLocation() {
	//首先检查权限
	// #ifdef APP-PLUS
	// 安卓检查权限有问题，暂时先不检查权限了
	// let appStatus = await checkPermission();
	// console.log('status', appStatus)
	// if (appStatus !== 1) {
	//     return;
	// }
	// #endif
	// #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ || MP-ALIPAY
	let status = await getSetting();
	if (status === 2) {
		uni.showModal({
			content: "爱上铁求访问您的位置信息",
			confirmText: "设置",
			success: function(res) {
				if (res.confirm) {
					openSetting();
				}
			},
		});
		return;
	}
	// #endif

	//然后发起定位
	return new Promise((resolve, reject) => {
		console.log("发起定位");
		// #ifdef APP-PLUS
		uni.getLocation({
			type: "gcj02", //wgs84 //gcj02
			// geocode: true,
			success: (res) => {
				console.log("APP定位成功：", res);
				// var address = res.address
				// address.latitude = res.latitude
				// address.longitude = res.longitude
				// let address = getCity(res)
				// console.log(111, address)
				// resolve();
				var cityModel = res
				var point = new plus.maps.Point(res.longitude, res.latitude);
				plus.maps.Map.reverseGeocode(
					point, {},
					function(event) {
						console.log('APP解析城市信息成功', event);
						var address = event.address; // 转换后的地理位置
						var reg = /.+?(省|市|自治区|自治州|县|区)/g;
				
						var addressList = address.match(reg).toString().split(",");
						console.log("=====adddd======", addressList);
						var city = addressList[0]
						if (city.indexOf("省") > -1) {
							city = addressList[1]
						}
				
						// if (city.substr(city.length - 1, 1) == '市') {
						// 	cityModel.city = city.substr(0, city.length - 1)
						// } else {
						// 	cityModel.city = city
						// }
						cityModel.city = city
						console.log("======currentCity======", cityModel);
						resolve(cityModel);
					},
					function(e) {}
				);
			},
			fail: (err) => {
				console.log("APP定位失败", err);
				reject(err);
			},
		});

		// #endif
		// #ifndef APP-PLUS
		//初始化高德SDK对象
		var amapPlugin = new amap.AMapWX({
			key: "e4d1c5fb48a0d2baa1d58325bd9361ef",
		});
		amapPlugin.getRegeo({
			success: (data) => {
				var city = data[0].regeocodeData.addressComponent.city;
				if (!city || city.length < 1) {
					city = data[0].regeocodeData.addressComponent.province;
				}
				console.log("小程序定位成功，定位城市：", city, ';  定位信息：', data);
				var address = data[0];
				address.city = city;
				resolve(address);
			},
			fail: (err) => {
				console.log("小程序定位失败", err);
				reject(err);
			},
		});
		// #endif
	});
}

function getSetting() {
	return new Promise((resolve, reject) => {
		uni.getSetting({
			success: (res) => {
				if (res.authSetting["scope.userLocation"] === undefined) {
					resolve(0);
					return;
				}
				if (res.authSetting["scope.userLocation"]) {
					resolve(1);
				} else {
					resolve(2);
				}
			},
		});
	});
}

function openSetting() {
	// this.hideConfirm();
	uni.openSetting({
		success: (res) => {
			if (res.authSetting && res.authSetting["scope.userLocation"]) {}
		},
		fail: (err) => {},
	});
}
async function checkPermission() {
	console.log("开始检查权限");
	let status = permision.isIOS ? await permision.requestIOS("location") : 1; //ios才做权限判断，安卓有问题，不在这里做权限判断
	console.log("权限status", status);
	if (status === null || status === 1) {
		status = 1;
	} else if (status === 2) {
		uni.showModal({
			content: "系统定位已关闭",
			confirmText: "确定",
			showCancel: false,
			success: function(res) {},
		});
	} else if (status.code) {
		uni.showModal({
			content: status.message,
		});
	} else {
		uni.showModal({
			content: "需要定位权限",
			confirmText: "设置",
			success: function(res) {
				if (res.confirm) {
					permision.gotoAppSetting();
				}
			},
		});
	}

	return status;
}


function getCity(res) {
	var cityModel = {}
	var that = this
	var point = new plus.maps.Point(res.longitude, res.latitude);
	return new Promise((resolve, reject) => {
		plus.maps.Map.reverseGeocode(
			point, {},
			function(event) {
				cityModel = event
				var address = event.address; // 转换后的地理位置
				console.log(address, 'address');
				var reg = /.+?(省|市|自治区|自治州|县|区)/g;

				var addressList = address.match(reg).toString().split(",");
				console.log("=====adddd======", addressList);
				var city = addressList[0]
				if (city.indexOf("省") > -1) {
					city = addressList[1]
				}

				// if (city.substr(city.length - 1, 1) == '市') {
				// 	cityModel.city = city.substr(0, city.length - 1)
				// } else {
				// 	cityModel.city = city
				// }
				cityModel.city = city
				console.log("======currentCity======", cityModel);
				resolve(cityModel);
			},
			function(e) {}
		);
	});
}
